﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Active.Core;

public partial class Media_MediaAdvances : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        { 
        }
    }

    protected void aspnetPager_PageChanged(object sender, EventArgs e)
    {
        gvAdvances.DataBind();
    }

    #region BusinessObject

    protected Media GetMedia()
    {
        string idString = Request.QueryString["id"];
        object businessObj = Session["CurrentObj"];
        if (businessObj == null
            || !(businessObj is Media)
            || ((Media)businessObj).Id.ToString() != idString)
        {
            try
            {
                if (!string.IsNullOrEmpty(idString))
                {
                    businessObj = Media.GetMedia(int.Parse(idString));
                    Session["CurrentObj"] = businessObj;
                }
                else
                {
                    Response.Redirect("~/Media/");
                }
            }
            catch (System.Security.SecurityException)
            {
                Response.Redirect("~/Media/");
            }
        }
        return businessObj as Media;
    }

    #endregion

    #region AdvanceInfoDataSource

    protected void AdvanceInfoDataSource_InsertObject(object sender, Csla.Web.InsertObjectArgs e)
    {
        Media obj = GetMedia();
        decimal payValue = Convert.ToDecimal(e.Values["PayValue"]);
        string description = e.Values["Description"] as string;
        obj.AddAdvance(payValue, description);

        Response.Redirect(Request.RawUrl, true);
    }

    #endregion

    #region AdvancesDataSource

    protected void AdvancesDataSource_SelectObject(object sender, Csla.Web.SelectObjectArgs e)
    {
        int pageNo = aspnetPager.CurrentPageIndex;
        int pageSize = aspnetPager.PageSize;
        int mediaId = GetMedia().Id;
        AdvanceList list = AdvanceList.GetAdvanceList(mediaId, pageNo, pageSize);
        aspnetPager.RecordCount = list.TotalRecordsCount;
        e.BusinessObject = list;
    }

    #endregion
    
}
